<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\know\validate;

use think\Validate;
use app\know\model\ResourceModel;

class ResourceValidate extends Validate
{
    protected $rule = [
        'url' => 'url|checkUrl',
        'id' => 'require|gt:0',
    ];

    protected $message = [
        'url.url' => '地址无效',
        'id.require' => 'id不能为空',
    ];

    protected $scene = [
        'add'  => ['url'],
        'edit' => ['id'],
        'delete' => ['id'],
    ];

    protected function checkUrl($value, $rule, $data)
    {
        $model = new ResourceModel();
        $item  = $model->where('url', $data['url'])
                       ->find();

        if (empty($item)) 
        {
            return true;
        } 
        else 
        {
            return $data['url'] . "---已经存在---";
        }
    }
}